package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Base64Util;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.app.entity.Xfc;
import org.springblade.modules.app.service.IXfcService;
import org.springblade.modules.system.entity.Region;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IRegionService;
import org.springblade.modules.system.service.IUserService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户表 控制器
 *
 * @author BladeX
 * @since 2020-10-19
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-app/usermgr")
@Api(value = "用户表", tags = "用户表接口")
public class UserMgrController extends BladeController {

    private final IUserService userMgrService;

    private final IRegionService regionService;

    private final IXfcService xfcService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入userMgr")
    public R<User> detail(User userMgr) {
        User detail = userMgrService.getOne(Condition.getQueryWrapper(userMgr));
        return R.data(detail);
    }

    /**
     * 分页 用户表
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入userMgr")
    public R<IPage<User>> list(Page page, User params) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>();
        if (!StringUtils.isEmpty(params.getName())) {
            queryWrapper.and(wrapper -> wrapper.eq(User::getName, params.getName()));
        }
        queryWrapper.and(wrapper -> wrapper.eq(User::getShstatus, "2"));
        queryWrapper.and(wrapper -> wrapper.eq(User::getUsertype, "2").or().eq(User::getUsertype, "3"));
        queryWrapper.orderByDesc(User::getCreateTime);
        IPage<User> pages = userMgrService.page(page, queryWrapper);
        return R.data(pages);
    }

    /**
     * 自定义分页 用户表
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入userMgr")
    public R<IPage<User>> page(Page page, User params) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>();
        if (!StringUtils.isEmpty(params.getName())) {
            queryWrapper.and(wrapper -> wrapper.eq(User::getName, params.getName()));
        }
        queryWrapper.and(wrapper -> wrapper.eq(User::getShstatus, "2"));
        queryWrapper.and(wrapper -> wrapper.eq(User::getUsertype, "2").or().eq(User::getUsertype, "3"));
        queryWrapper.orderByDesc(User::getCreateTime);
        IPage<User> pages = userMgrService.page(page, queryWrapper);
        return R.data(pages);
    }


    /**
     * 新增或修改 用户表
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入userMgr")
    public R submit(@Valid @RequestBody User userMgr) {
        Region regionP = regionService.getById(userMgr.getProncecode());
        userMgr.setProncename(regionP.getName());

        if (!StringUtils.isEmpty(userMgr.getCitycode())) {
            Region regionC = regionService.getById(userMgr.getCitycode());
            userMgr.setCityname(regionC.getName());
        }

        if (!StringUtils.isEmpty(userMgr.getAreacode())) {
            Region regionA = regionService.getById(userMgr.getAreacode());
            userMgr.setAreaname(regionA.getName());
        }
        List<Long> longList = Func.toLongList(Base64Util.decode(userMgr.getQpccode()));
        String qpcname = "";
        for (Long qpcid : longList) {
            Xfc xfc = xfcService.getById(qpcid);
            qpcname = qpcname + xfc.getName() + ",";
        }
        userMgr.setQpccode(Base64Util.decode(userMgr.getQpccode()));
        userMgr.setQpcname(qpcname);
        return R.status(userMgrService.saveOrUpdate(userMgr));
    }


    /**
     * 删除 用户表
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        ////TODO 待开放有哪些用户可以删除
        //FIXME  针对于商家
        // 1. 商家有下单的 订单能删除吗  已接单的 已完工的 未支付的 已取消的 未支付违约金的
        //FIXME 针对工人
        // 2. 工人有违约的 新接订单的  工人有账户里有钱的  有未支付违约金的

        // 3. 现在需要放开吗  注意下不要误操作
        return R.status(userMgrService.removeByIds(Func.toLongList(ids)));
    }


}
